6 Lecture

CS201

Midterm & Final Term Short Notes

Repetition Structure (Loop)

Repetition structure, also known as loops, is a programming construct that allows a program to execute a certain section of code repeatedly, based on a certain condition or set of conditions. Loops enable programmers to write code that performs


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which of the following is not a type of loop structure? A) For loop B) While loop C) Repeat loop D) Do-while loop Answer: C) Repeat loop

  2. Which loop structure is best suited for situations where the loop must be executed at least once? A) For loop B) While loop C) Do-while loop D) All of the above Answer: C) Do-while loop

  3. Which loop structure executes its body at least once before evaluating the loop condition? A) For loop B) While loop C) Do-while loop D) All of the above Answer: C) Do-while loop

  4. Which loop structure is typically used when the number of iterations is known in advance? A) For loop B) While loop C) Do-while loop D) All of the above Answer: A) For loop

  5. Which keyword is used to skip the rest of the current iteration in a loop structure? A) Continue B) Break C) Skip D) Exit Answer: A) Continue

  6. Which keyword is used to exit the loop structure immediately? A) Continue B) Break C) Skip D) Exit Answer: B) Break

  7. Which of the following statements is true regarding nested loop structures? A) Nested loop structures are not allowed in most programming languages. B) Nested loop structures can only be used with for loops. C) Nested loop structures can increase the time complexity of a program. D) None of the above. Answer: C) Nested loop structures can increase the time complexity of a program.

  8. Which of the following is an infinite loop? A) for (int i = 0; i < 10; i++) B) while (true) {} C) do { } while (false); D) None of the above. Answer: B) while (true) {}

  9. Which loop structure can be used to iterate over elements of an array? A) For loop B) While loop C) Do-while loop D) All of the above Answer: A) For loop

  10. Which loop structure is best suited for situations where the number of iterations is not known in advance? A) For loop B) While loop C) Do-while loop D) All of the above Answer: B) While loop



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a loop in programming? Answer: A loop is a programming construct that allows a section of code to be executed repeatedly based on a certain condition or set of conditions.

  2. What is the difference between a while loop and a do-while loop? Answer: In a while loop, the loop condition is evaluated before the loop body is executed, whereas in a do-while loop, the loop body is executed at least once before the loop condition is evaluated.

  3. What is the purpose of a break statement in a loop structure? Answer: The break statement is used to immediately exit the loop structure, even if the loop condition has not been met.

  4. What is the purpose of a continue statement in a loop structure? Answer: The continue statement is used to skip the rest of the current iteration of the loop and move on to the next iteration.

  5. What is a nested loop and how does it work? Answer: A nested loop is a loop structure that contains another loop structure within it. The outer loop executes the inner loop multiple times, which can increase the time complexity of the program.

  6. What is the syntax for a for loop? Answer: The syntax for a for loop is: for (initialization; condition; increment/decrement) { loop body }

  7. What is an infinite loop and how is it created? Answer: An infinite loop is a loop structure that runs indefinitely without stopping. It can be created by using a loop condition that always evaluates to true, or by not including a break statement inside the loop body.

  8. What is the difference between a pre-test loop and a post-test loop? Answer: A pre-test loop (such as a for loop or a while loop) evaluates the loop condition before the loop body is executed, whereas a post-test loop (such as a do-while loop) evaluates the loop condition after the loop body is executed.

  9. What is the difference between a for loop and a foreach loop? Answer: A for loop is used to iterate a fixed number of times, whereas a foreach loop is used to iterate over the elements of a collection, such as an array.

  10. How can you break out of multiple nested loops in one statement? Answer: You can use labeled break statements to break out of multiple nested loops in one statement.

In programming, loops are used to execute a block of code repeatedly. They are an important construct for performing tasks such as iterating over arrays or lists, repeating a set of instructions until a certain condition is met, or executing code a specific number of times. There are several types of loop structures, including the for loop, while loop, and do-while loop. Each loop structure has its own syntax and use cases, but they all allow code to be executed repeatedly until a certain condition is met. The for loop is used when the number of iterations is known in advance. It has a loop variable, a loop condition, and an increment or decrement statement. The loop variable is initialized, the condition is checked, and the loop body is executed repeatedly until the condition is false. The while loop is used when the number of iterations is not known in advance. It has a loop condition that is checked before the loop body is executed. If the condition is true, the loop body is executed, and the condition is checked again. The loop continues until the condition is false. The do-while loop is similar to the while loop, but the loop body is executed at least once before the loop condition is checked. This means that the loop body will always execute at least once, even if the loop condition is false from the beginning. Loops can be nested, meaning that one loop structure can be inside another loop structure. This is useful for performing complex tasks that require multiple iterations or for iterating over multidimensional arrays. In addition to loop structures, there are also break and continue statements that can be used inside loops. The break statement allows you to exit the loop immediately, while the continue statement allows you to skip the current iteration and move on to the next one. Overall, loops are a powerful and important construct in programming, and understanding how to use them effectively is essential for writing efficient and effective code.